﻿#git拉取工程
#修改工程名,设置外链
#修改自己的名字

import os,sys,shutil,re
import main

from sys import argv

DesGit = None# "https://gitee.com/TanLvXu258/testGit.git" #目的仓库
DesBranch = 'master'
TepGit = "https://gitee.com/115088853/ddz_client_new.git"#"https://gitee.com/TanLvXu258/PokerProjectTemplate.git" #模板仓库
DesPath = None ;
ProjectName = None;
if len(argv) > 1:
	ProjectName = argv[1]
	DesPath = argv[2];
	if len(argv) >= 4:
		DesGit = argv[3];

#拉取模板git
def initgit(desPath):
	if not os.path.exists(desPath):
		os.makedirs(desPath)
	os.chdir(desPath);
	if DesGit:	
		os.system(' git init');
		os.system(' git remote add origin ' + DesGit);
		os.system(' git pull origin master ');
	os.system(' git clone ' + TepGit + ' -b '+ DesBranch +' template');	
	main.copyProjectFirst(os.path.join(desPath,'template'),desPath,'ddz',ProjectName);
	if DesGit:
		main.initsubgit(os.path.join(desPath,'template'),desPath,'ddz',ProjectName);

def CleanDir( Dir ):
    if os.path.isdir( Dir ):
        paths = os.listdir( Dir )
        for path in paths:
            filePath = os.path.join( Dir, path )
            if os.path.isfile( filePath ):
                try:
                    os.remove( filePath )
                except os.error:
                    print( "remove %s error." %filePath )#引入logging
            elif os.path.isdir( filePath ):
                if filePath[-4:].lower() == ".svn".lower():
                    continue
                CleanDir(filePath)
    return True


def delTemplate(desPath):
	os.chdir(desPath);
	try:
		os.system('rd /s /q template')
	except os.error:
		print("not support this os");
	CleanDir(os.path.join(desPath,'template'))
	os.startfile(desPath)
	
#修改相对路径为绝对路径
def change_real_dir(path):
	if not path:
		return 
	basePath = sys.path[0]
	group = path.split('../')
	print(group)
	deep = len(group) - 1;
	desPath = group[deep]
	rootPath = basePath;
	while deep>=1:
		rootPath,_ = os.path.split(rootPath)
		deep = deep -1 
		#print("rootPath",rootPath)
		#print("desPath",desPath)
	return os.path.join(rootPath,desPath);


print("start create project!")
DesPath = change_real_dir(DesPath)
if not os.path.exists(DesPath):
	os.makedirs(DesPath)
initgit(DesPath)
delTemplate(DesPath);
print("create project complete!")
#print(project)

